package ru.spbu.math.android.cbrf.dataprovider.handlers;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import ru.spbu.math.android.cbrf.model.StockItem;

import java.util.ArrayList;
import java.util.List;

/**
 * Class for parsing data from CBRF and put it in the List of StockItem This class extends DefaultHandler
 * @author  Svetlana Zemlyanskaya
 */

public class StockItemSaxHandler extends DefaultHandler {

    private final String ID = "ID";
    private final String CHAR_CODE = "charCode";
    private final String NOMINAL = "nominal";
    private final String NAME = "name";
    private final String VALUTE = "valute";

    private List<StockItem> itemList;
    /**
	 * @uml.property  name="item"
	 * @uml.associationEnd  
	 */
    private StockItem item;
    private StringBuilder builder;

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        builder.append(ch, start, length);
    }

    @Override
    public void endElement(String uri, String localName, String name)
            throws SAXException {
        if (this.item != null) {
            if (localName.equalsIgnoreCase(CHAR_CODE)) {
                item.setCode(getNameFromString(builder.toString()));
            } else if (localName.equalsIgnoreCase(NOMINAL)) {
                item.setNominalValue(getIntFromString(builder.toString()));
            } else if (localName.equalsIgnoreCase(NAME)) {
                String argument = getNameFromString(builder.toString());
                item.setName(argument);
                item.setNominalName(argument);
            } else if (localName.equalsIgnoreCase(VALUTE)) {
                itemList.add(item);
            }

            builder.setLength(0);
        }
    }

    @Override
    public void startDocument() throws SAXException {
        itemList = new ArrayList<StockItem>();
        builder = new StringBuilder();
    }

    @Override
    public void startElement(String uri, String localName, String name,
                             Attributes attributes) throws SAXException {
        if (localName.equalsIgnoreCase(VALUTE)) {
            this.item = new StockItem();
            item.setId(attributes.getValue(ID));
        }
    }

    /**
	 * Return StockItem list from XML
	 * @return  List<StockItem>
	 * @uml.property  name="item"
	 */
    public List<StockItem> getItem() {
        return itemList;
    }

    //Take int from specified string

    private static int getIntFromString(String line) {
        String str = "";
        boolean start = false, stop = false;
        int result = 0;

        for (int i = 0; i < line.length(); i++) {
            if (Character.isDigit(line.charAt(i)) && !stop) {
                start = true;
                str += line.charAt(i);

            } else if (start) {
                stop = true;
            }
        }
        if (start) {
            result = Integer.parseInt(str);
        } else {
            result = 0;
        }

        return result;

    }

    //Take name from specified string

    private static String getNameFromString(String line) {
        String str = "";
        boolean startName = false, start = false;
        String result = "";
        List<String> resultList = new ArrayList<String>();

        for (int i = 0; i < line.length(); i++) {
            if (Character.isDigit(line.charAt(i)) || Character.isLetter(line.charAt(i))) {
                startName = true;
                start = true;
                str += line.charAt(i);
            } else if (start) {
                start = false;
                resultList.add(str);
                str = "";
            }
        }
        if (start) {
            resultList.add(str);
        }
        if (startName) {
            for (int i = 0; i < resultList.size() - 1; i++) {
                result += resultList.get(i) + " ";
            }
            result += resultList.get(resultList.size() - 1);
        } else {
            result = "";
        }
        return result;
    }
}
